﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Markup;
using NetBay.Core.BusinessEntities;
using Telerik.Windows.Controls;

namespace Rio.Framework.Pages.AppCode.Models
{
    [ContentProperty("Children")]
    public class DataItemTreeView : ViewModelBase
    {

        #region Fields

        private string _text;
        private FontWeight _textFont = FontWeights.Normal;
        private bool _isExpanded;
        private bool _isSelected;
        private bool _isDropAllowed;
        private DataItemTreeView _parent;
        private DataItemTreeViewCollection _items;
        private string _code;
        private List<Document> _listDocument;
        private bool _isTrashItem;
        private bool _canBeConsult = false;
        private bool _canBeModify = false;
        private bool _canSubItemBeConsultOrModify = false;
        private bool _isDefaultItem;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the is default item.
        /// </summary>
        /// <value>The is default item.</value>
        public bool IsDefaultItem
        {
            get
            {
                return _isDefaultItem;
            }

            set
            {
                _isDefaultItem = value;
                this.OnPropertyChanged("IsDefaultItem");
            }
        }

        /// <summary>
        /// Gets or sets the is trash item.
        /// </summary>
        /// <value>The is trash item.</value>
        public bool IsTrashItem
        {
            get
            {
                return _isTrashItem;
            }

            set
            {
                _isTrashItem = value;
                this.OnPropertyChanged("IsTrashItem");
            }
        }

        /// <summary>
        /// Gets or sets the can be consult.
        /// </summary>
        /// <value>The can be consult.</value>
        public bool CanBeConsult
        {
            get
            {
                return this._canBeConsult;
            }
            set
            {
                this._canBeConsult = value;
            }
        }

        /// <summary>
        /// Gets or sets the can be modify.
        /// </summary>
        /// <value>The can be modify.</value>
        public bool CanBeModify
        {
            get
            {
                return this._canBeModify;
            }
            set
            {
                this._canBeModify = value;
            }
        }

        /// <summary>
        /// Gets or sets the can sub item be consult or modify.
        /// </summary>
        /// <value>The can sub item be consult or modify.</value>
        public bool CanSubItemBeConsultOrModify
        {
            get
            {
                return this._canSubItemBeConsultOrModify;
            }
            set
            {
                this._canSubItemBeConsultOrModify = value;
            }
        }

        /// <summary>
        /// Gets or sets the items count.
        /// </summary>
        /// <value>The items count.</value>
        public int ItemsCount
        {
            get
            {
                if (this.ListDocument != null)
                    return this.ListDocument.Count;

                return 0;
            }
        }

        /// <summary>
        /// Gets or sets the text font.
        /// </summary>
        /// <value>The text font.</value>
        public FontWeight TextFont
        {
            get
            {
                return this._textFont;
            }
            set
            {
                _textFont = value;
                this.OnPropertyChanged("TextFont");
            }
        }

        /// <summary>
        /// Gets or sets the code.
        /// </summary>
        /// <value>The code.</value>
        public string Code
        {
            get
            {
                return this._code;
            }
            set
            {
                if (this._code != value)
                {
                    this._code = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get
            {
                if (this._text != null)
                {
                    string[] l_sTextArray = _text.Split('(');
                    if (ItemsCount > 0)
                    {
                        _text = l_sTextArray[0] + "(" + ItemsCount + ")";
                        TextFont = FontWeights.Bold;
                    }
                    else
                    {
                        _text = l_sTextArray[0];
                        TextFont = FontWeights.Normal;
                    }
                }
                else
                {
                    TextFont = FontWeights.Normal;
                }

                return _text;
            }
            set
            {
                if (this._text != value)
                {
                    OriginalName = value;
                    _text = value;
                    this.OnPropertyChanged("Text");
                }
            }
        }

        public string OriginalName { get; private set; }

        /// <summary>
        /// Gets or sets the parent.
        /// </summary>
        /// <value>The parent.</value>
        public DataItemTreeView Parent
        {
            get
            {
                return this._parent;
            }
            set
            {
                this._parent = value;
            }
        }

        /// <summary>
        /// Gets or sets the items.
        /// </summary>
        /// <value>The items.</value>
        public DataItemTreeViewCollection Items
        {
            get
            {
                return this._items;
            }

            set
            {
                if (this._items != value)
                {
                    this._items = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the b is expanded.
        /// </summary>
        /// <value>The b is expanded.</value>
        public bool bIsExpanded
        {
            get
            {
                return this._isExpanded;
            }
            set
            {
                if (this._isExpanded != value)
                {
                    this._isExpanded = value;
                    this.OnPropertyChanged("bIsExpanded");
                }
            }
        }

        /// <summary>
        /// Gets or sets the b is selected.
        /// </summary>
        /// <value>The b is selected.</value>
        public bool bIsSelected
        {
            get
            {
                return this._isSelected;
            }
            set
            {
                if (this._isSelected != value)
                {
                    this._isSelected = value;
                    this.OnPropertyChanged("bIsSelected");
                }
            }
        }

        /// <summary>
        /// Gets or sets the b is drop allowed.
        /// </summary>
        /// <value>The b is drop allowed.</value>
        public bool bIsDropAllowed
        {
            get
            {
                return this._isDropAllowed;
            }
            set
            {
                if (this._isDropAllowed != value)
                {
                    this._isDropAllowed = value;
                    this.OnPropertyChanged("bIsDropAllowed");
                }
            }
        }

        /// <summary>
        /// Gets or sets the list document.
        /// </summary>
        /// <value>The list document.</value>
        public List<Document> ListDocument
        {
            get
            {
                return this._listDocument;
            }
            set
            {
                if (this._listDocument != value)
                {
                    this._listDocument = value;
                    this.OnPropertyChanged("ListDocument");
                    this.OnPropertyChanged("Text");
                }
            }
        }

        #endregion

        #region Constructors 

        /// <summary>
        /// Initializes a new instance of the <see cref="DataItemTreeView" /> class.
        /// </summary>
        public DataItemTreeView()
        {
            this._items = new DataItemTreeViewCollection(this);
        }
        #endregion
    }
}